<template>
  <section class="f-order-sale">
    <div class="w-card my-content-box" v-if="!isDetail">
      <div class="title-box">
        <span class="title-text">退款订单</span>
        <div style="margin-left:auto;">
          <el-button type="primary" plain @click="importHandle()">导入</el-button>
        </div>
      </div>
      <div class="my-search-box">
        <div>
          <span>订单ID：</span>
          <el-input v-model="searchForm.rechargeOrderId" clearable placeholder="请输入订单ID" />
        </div>
        <div>
          <span>用户码：</span>
          <el-input v-model="searchForm.userCode" clearable placeholder="请输入用户码" />
        </div>
        <div>
          <span>订单状态：</span>
          <el-select v-model="searchForm.chargesStatus" clearable>
            <el-option v-for="item in orderStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
        <div>
          <span>套餐：</span>
          <el-select v-model="searchForm.packageProductIds" multiple clearable>
            <el-option v-for="item in packageList" :key="item.packageId" :label="item.packageName" :value="item.packageId" />
          </el-select>
        </div>
      </div>
      <div class="my-search-box">
        <div>
          <span>关联商户：</span>
          <el-select v-model="searchForm.shopId" clearable>
            <el-option v-for="item in shopList" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
          </el-select>
        </div>
        <div class="datebox">
          <span>支付时间：</span>
          <el-date-picker v-model="searchForm.date" type="datetimerange" value-format="x" start-placeholder="开始时间"
            end-placeholder="结束时间" @change="(e) => {console.log(e)}" />
        </div>
        <div class="f-ml-20">
          <el-button @click="searchHandle" type="primary">查询</el-button>
          <el-button @click="resetHandle">重置</el-button>
        </div>
      </div>
      <div class="my-table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'12px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="rechargeOrderId" label="订单id" align="center" width="180">
            <template v-slot="scope">
              <el-link style="fontSize:12px" type="primary" @click="lookDetail(scope.row.rechargeOrderId)">
                {{ scope.row.rechargeOrderId || '' }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column prop="chargesStatus" label="订单状态" align="center">
            <template v-slot="scope">
              {{ chargesStatus[scope.row.chargesStatus] }}
            </template>
          </el-table-column>
          <el-table-column prop="userCode" label="用户码" align="center">
            <template v-slot="scope">
              {{ scope.row.userCode || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="shopName" label="关联商户" align="center">
            <template v-slot="scope">
              {{ scope.row.shopName || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="agent" label="代理商" width="250">
            <template v-slot="scope">
              {{ scope.row.agent || '--' }}
            </template>
          </el-table-column>
          <el-table-column label="套餐名称" width="200">
            <template v-slot="scope">
              <div>{{ scope.row.packageProduct || '--' }}</div>
            </template>
          </el-table-column>
          <el-table-column label="套餐类别" width="200">
            <template v-slot="scope">
              <div>{{ scope.row.packageInfo || '--' }}</div>
            </template>
          </el-table-column>
          <el-table-column label="支付信息" width="300">
            <template v-slot="scope">
              <div class="g-flex-ct">
                <p>订单金额：</p>
                <p style="color:#FF4040">¥ {{ scope.row.money/100 || '--' }}</p>
              </div>
              <div class="g-flex-ct">
                <p>支付渠道：</p>
                <p>{{ scope.row.payChannel || '--' }}</p>
              </div>
              <div class="g-flex-ct">
                <p>支付方式：</p>
                <p>{{ scope.row.payWay || '--'}}</p>
              </div>
              <div class="g-flex-ct">
                <p>支付时间：</p>
                <p>{{ getDateTime(scope.row.payTime)[6] || '' }}</p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="my-table-footer">
          <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
            :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>

    <!-- 导入弹窗 -->
    <Upload ref="uploadRef" v-if="off.upload" :uploadForm="uploadForm" @handleUploadSuccess="importSuccess"
      @handleUploadCancel="off.upload = false" />

    <router-view></router-view>
  </section>
</template>

<script>
import { onMounted, reactive, ref, toRefs, watch, nextTick } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, successDeal, BlobDownLoad } from '@/utils/utils'
import { useRoute, useRouter } from 'vue-router'
import Upload from '@/components/Upload.vue'
import api from '../api'

export default {
  components: { Upload },

  setup() {
    const route = useRoute()
    const router = useRouter()

    const uploadRef = ref(null)

    const state = reactive({
      off: {
        upload: false,
      },
      isDetail: false,

      searchForm: {
        "chargesStatus": "",
        "orderBy": "",
        "packageProductIds": [],
        "rechargeOrderId": "",
        "userCode": ""
      },
      searchFormDeep: {},

      tableHeight: 0,
      tableData: [],
      page: 1,
      size: 10,
      total: 0,

      orderStatusOptions: [
        { value: 1, label: '待处理' },
        { value: 2, label: '佣金已退' },
        { value: 3, label: '无需退佣金' },
        { value: 4, label: '退佣金失败' },
      ],
      chargesStatus: {
        '1': '待处理',
        '2': '佣金已退',
        '3': '无需退佣金',
        '4': '退佣金失败',
      },

      packageList: [],
      shopList: [],

      uploadForm: {
        dialogTitle: '导入退款订单',
        action: '/eas/manage/recharge/refund'
      }
    })

    watch(() => route.path, (newVal, oldVal) => {
      setIsDetail()
    })

    function setIsDetail() {
      state.isDetail = (route.path != '/rechargeRefund')
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = JSON.parse(JSON.stringify(state.searchFormDeep))
      searchHandle()
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        createTimeMin: (state.searchForm.date && state.searchForm.date[0]) || '',
        createTimeMax: (state.searchForm.date && state.searchForm.date[1]) || '',
        orderBy: 'desc',
      }
      api.orderRechargeRefund(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function lookDetail(rechargeOrderId) {
      router.push({ path: '/rechargeRefund/detail', query: { rechargeOrderId } })
    }

    const getPackageAll = () => {
      state.packageList = []
      api.packageAll().then(res => {
        state.packageList = res.data
      })
    }

    //导入成功
    function importSuccess() {
      state.page = 1
      getTableData()
    }

    function importHandle() {
      state.off.upload = true
      nextTick(() => {
        uploadRef.value.off.dialog = true
      })
    }

    const getShopAll = () => {
      api.shopAll().then(res => {
        state.shopList = res.data
      })
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 330
      state.searchFormDeep = JSON.parse(JSON.stringify(state.searchForm))
      setIsDetail()
      getTableData()
      getPackageAll()
      getShopAll()
    })

    return {
      ...toRefs(state),
      uploadRef,
      searchHandle,
      resetHandle,
      lookDetail,
      handleCurrentChange,
      handleSizeChange,
      getDateTime,
      getPackageAll,
      importSuccess,
      importHandle,
    }
  }
}
</script>

<style lang="scss" scoped>
.f-order-sale {
  .my-content-box {
    box-sizing: border-box;
    padding: 20px;

    > .title-box {
      display: flex;
      align-items: center;

      > .title-text {
        font-size: 16px;
        font-weight: bold;
      }
    }

    .my-search-box {
      box-sizing: border-box;
      padding: 20px 20px 20px 0;
      padding-bottom: 0;
      font-size: 14px;
      display: flex;
      align-items: center;
      gap: 10px;

      > div {
        width: 300px;
        display: flex;
        align-items: center;

        > span {
          flex-shrink: 0;
          width: 90px;
          text-align: right;
        }
      }

      .datebox {
        width: 400px;
      }
    }

    .my-table-box {
      margin-top: 20px;

      .table-handle-box {
        display: flex;
        justify-content: space-around;
        align-items: center;
      }

      .my-table-footer {
        display: flex;
        align-items: center;
        margin-top: 10px;
        flex-wrap: wrap;
        gap: 10px;
      }
    }
  }
}
</style>